home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Frameworks / Argus Frameworks 2.1 / Argus Libraries 2.1 / FnMisc.cp < prev    next >
Text File  |  1995-12-19  |  7KB  |  267 lines

  1. /**********************************************************************
  2.  
  3.     FnMisc.cp
  4.  
  5. ***********************************************************************/
  6.  
  7. /*
  8.     Here are some miscellaneous functions which tend to be used a lot.
  9.     
  10.     Functions Include:
  11.     
  12.       FnMisc_ReadPrefs          Reads string from res, converts to int
  13.       FnMisc_SavePrefs          Stores int as a string in res fork
  14.       FnMisc_ColorAvailability  Checks for Color QuickDraw
  15.       FnMisc_GetPixelDepth      Returns current monitor setting
  16.       FnMisc_FrameButton        Frames default dialog button
  17.       FnMisc_TitleBarHeight     Returns titlebar height of window
  18.       FnMisc_LeftBorderWidth    Returns left border width of window
  19.       FnMisc_RightBorderWidth   Returns right (and bottom) border width
  20. */
  21.  
  22. // Prototypes
  23.  
  24. long    FnMisc_ReadPrefs         ( int prefStrID );
  25. void    FnMisc_SavePrefs         ( int prefStrID, long value );
  26. Boolean FnMisc_ColorAvailability ( void );
  27. int     FnMisc_GetPixelDepth     ( GDHandle theDevice );
  28. void    FnMisc_FrameButton       ( DialogPtr theDialog,
  29.                                    short buttonID );
  30. int     FnMisc_TitleBarHeight    ( WindowPtr w );
  31. int     FnMisc_LeftBorderWidth   ( WindowPtr w );
  32. int     FnMisc_RightBorderWidth  ( WindowPtr w );
  33.  
  34. /********** ReadPrefs */
  35.  
  36. long FnMisc_ReadPrefs( int prefStrID )
  37. /*
  38.     Reads in a string from resource fork specified by prefStrID, 
  39.     converts it to an integer, and returns result.
  40. */
  41. {
  42.     StringHandle  prefStrH;
  43.     Str255        prefStr;
  44.     unsigned char *tempStr;
  45.     int           strLength, defaultResult, i;
  46.     long          result;
  47.     
  48.     defaultResult = 0;
  49.     if( (prefStrH = GetString( prefStrID )) == NULL )
  50.     {
  51.         result = defaultResult;
  52.     }
  53.     else
  54.     {
  55.         HLock( (Handle)prefStrH );
  56.         strLength = (int)(**prefStrH);
  57.         tempStr = *prefStrH;
  58.         for( i=0; i<=strLength; i++ )
  59.         {
  60.             prefStr[i] = tempStr[i];
  61.         }
  62.         StringToNum( prefStr, &result );
  63.         HUnlock( (Handle)prefStrH );
  64.     }
  65.     return result;
  66. }
  67.  
  68.  
  69. /********** SavePrefs */
  70.  
  71. void FnMisc_SavePrefs( int prefStrID, long value )
  72. /*
  73.     Takes a value, converts it into an string, and saves it into an
  74.     existing resource 'STR' identified by prefStrID.  The length of
  75.     the string must be less than or equal to the existing string
  76.     length.  If smaller, string is padded with leading spaces so
  77.     string length in resource is left unchanged.
  78. */
  79. {
  80.     StringHandle  prefStrH;
  81.     Str255        prefStr, valueStr;
  82.     unsigned char *tempStr;
  83.     int           strLength, numLength, i;
  84.  
  85.     if( (prefStrH = GetString( prefStrID )) == NULL )
  86.     {
  87.         // do nothing, string doesn't exist (add error routine?)
  88.     }
  89.     else
  90.     {
  91.         HLock( (Handle)prefStrH );
  92.         strLength = (int)(**prefStrH);
  93.         tempStr = *prefStrH;
  94.         NumToString( value, valueStr );
  95.         numLength = (int)(*valueStr);
  96.         if( numLength <= strLength )
  97.         {
  98.             for( i=1; i<=strLength; i++ )
  99.                 tempStr[i] = ' ';
  100.             for( i=(strLength - numLength + 1); i<=strLength; i++ )
  101.                 tempStr[i] = valueStr[i-(strLength-numLength)];
  102.         }
  103.         ChangedResource( (Handle)prefStrH );
  104.         WriteResource( (Handle)prefStrH );
  105.         HUnlock( (Handle)prefStrH );
  106.     }
  107. }
  108.  
  109.  
  110. /********** ColorAvailability */
  111.  
  112. Boolean FnMisc_ColorAvailability( void )
  113. /*
  114.     Checks to see if the current machine supports Color QuickDraw.  Use
  115.     this routine once at the beginning of your program.
  116.  
  117.     This routine is obsolete, use gSysConfig.hasColorQD variable from
  118.     Argus Starter : Main.cp.  Kept here for versions 2.x of Argus
  119.     Libraries for compatability.
  120. */
  121. {
  122.     SysEnvRec mySystem;
  123.  
  124.     SysEnvirons( 2, &mySystem );
  125.     return( mySystem.hasColorQD );
  126. }
  127.  
  128.  
  129. /********** GetPixelDepth */
  130.  
  131. int FnMisc_GetPixelDepth( GDHandle theDevice )
  132. /*
  133.     Returns the current pixel depth setting of the machine.  Since the
  134.     user can change the setting of the pixel depth on-the-fly (using 
  135.     the 'Monitor' control panel), this routine should be called each
  136.     time you do any drawing.
  137.  
  138.     Example Usage:
  139.         GDHandle  gCurrentDevice;
  140.         int       gPixelDepth;
  141.  
  142.         gCurrentDevice = GetDeviceList();
  143.         gPixelDepth = GetPixelDepth( gCurrentDevice );
  144. */
  145. {
  146.     PixMapHandle screenPMapH;
  147.     int          pixelDepth;
  148.  
  149.     screenPMapH = (**theDevice).gdPMap;
  150.     pixelDepth = (**screenPMapH).pixelSize;
  151.     return( pixelDepth );
  152. }
  153.  
  154.  
  155. /********** FrameButton */
  156. /*
  157.     Frames a button (usually the OK button) in a dialog.
  158. */
  159. void FnMisc_FrameButton( DialogPtr theDialog, short buttonID )
  160. {
  161.     const int kButtonFrameInset = -4;
  162.     const int kButtonFrameSize = 3;
  163.     const int kFilletSize = 16;
  164.  
  165.     short    itemType;
  166.     Rect     itemRect;
  167.     Handle   itemHandle;
  168.     PenState thePnState;
  169.     GrafPtr  oldPort;
  170.     
  171.     GetPort( &oldPort );
  172.     SetPort( theDialog );
  173.     GetDItem( theDialog, buttonID, &itemType, &itemHandle, &itemRect );
  174.     GetPenState( &thePnState );
  175.     PenNormal();
  176.     PenSize( kButtonFrameSize, kButtonFrameSize );
  177.     InsetRect( &itemRect, kButtonFrameInset, kButtonFrameInset );
  178.     FrameRoundRect( &itemRect ,kFilletSize, kFilletSize );
  179.     SetPenState( &thePnState );
  180.     SetPort( oldPort );
  181. }
  182.  
  183.  
  184. /********** TitleBarHeight */
  185. /*
  186.     Returns height of window titlebar in pixels.  Reference IM
  187.     'Macintosh Toolbox Essentials', listing 4-12, page 4-55.
  188. */
  189. int FnMisc_TitleBarHeight( WindowPtr w )
  190. {
  191.     int        titleBarHeight;
  192.     Rect       wRect;
  193.     WindowPeek wPeek;
  194.     GrafPtr    oldPort;
  195.     Point      pt;
  196.  
  197.     GetPort( &oldPort );
  198.     SetPort( w );
  199.     wRect = w->portRect;
  200.     pt.h = wRect.left;
  201.     pt.v = wRect.top;
  202.     LocalToGlobal( &pt );
  203.     wRect.left = pt.h;
  204.     wRect.top = pt.v;
  205.     wPeek = (WindowPeek)w;
  206.     // following line different from IM in that '1' is not subtracted
  207.     // from equation.  Count the bits yourself, it works.
  208.     titleBarHeight = wRect.top - (*(wPeek->strucRgn))->rgnBBox.top;
  209.     SetPort( oldPort );
  210.     return titleBarHeight;
  211. }
  212.  
  213.  
  214. /********** LeftBorderWidth */
  215. /*
  216.     Returns width of left window border in pixels.
  217. */
  218. int FnMisc_LeftBorderWidth( WindowPtr w )
  219. {
  220.     int        borderWidth;
  221.     Rect       wRect;
  222.     WindowPeek wPeek;
  223.     GrafPtr    oldPort;
  224.     Point      pt;
  225.  
  226.     GetPort( &oldPort );
  227.     SetPort( w );
  228.     wRect = w->portRect;
  229.     pt.h = wRect.left;
  230.     pt.v = wRect.top;
  231.     LocalToGlobal( &pt );
  232.     wRect.left = pt.h;
  233.     wRect.top = pt.v;
  234.     wPeek = (WindowPeek)w;
  235.     borderWidth = wRect.left - (*(wPeek->strucRgn))->rgnBBox.left;
  236.     SetPort( oldPort );
  237.     return borderWidth;
  238. }
  239.  
  240.  
  241. /********** RightBorderWidth */
  242. /*
  243.     Returns width of right window border in pixels.
  244. */
  245. int FnMisc_RightBorderWidth( WindowPtr w )
  246. {
  247.     int        borderWidth;
  248.     Rect       wRect;
  249.     WindowPeek wPeek;
  250.     GrafPtr    oldPort;
  251.     Point      pt;
  252.  
  253.     GetPort( &oldPort );
  254.     SetPort( w );
  255.     wRect = w->portRect;
  256.     pt.h = wRect.right;
  257.     pt.v = wRect.bottom;
  258.     LocalToGlobal( &pt );
  259.     wRect.right = pt.h;
  260.     wRect.bottom = pt.v;
  261.     wPeek = (WindowPeek)w;
  262.     borderWidth = (*(wPeek->strucRgn))->rgnBBox.right - wRect.right;
  263.     SetPort( oldPort );
  264.     return borderWidth;
  265. }
  266.  
  267. // End of File